﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.CarModel
{
    public partial class Accident : BaseForm
    {
        AccidentSvc.SoapHeaderClass sopaHeader = new AccidentSvc.SoapHeaderClass();
        private bool isDriver = false;
        private string strSearch = " 1=1";
        public Accident()
        {
            InitializeComponent();
        }
        public Accident(bool isDri)
        {
            InitializeComponent();
            isDriver = isDri;
        }
        private int dgvBind()
        {
            string _strSql = "";
            string strWhere = strSearch;
            PageData pageData = new PageData(this,pager1,this.overlay);
            pageData.TableName = "Accident";
            pageData.PrimaryKey = "Id";
            pageData.OrderStr = "Id desc";
            pageData.PageIndex = this.pager1.PageCurrent;
            pageData.PageSize = this.pager1.PageSize;
            pageData.QueryCondition = _strSql + strWhere.ToString();
            pageData.QueryFieldName = "*";

            dgvInfo.AutoGenerateColumns = false;
            DataSet ds = pageData.QueryDataTable();
            dgvInfo.DataSource = this.pager1.bindingSource;
            return pageData.TotalCount;
        }
        private void Accident_Load(object sender, EventArgs e)
        {
            this.pager1.PageCurrent = 1;
            this.pager1.Bind();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddAccident add = new AddAccident();
            add.ShowDialog();
            this.pager1.Bind();
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            strSearch = "";
            this.pager1.Bind();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            if (!string.IsNullOrEmpty(txtCarCode.Text))
            {
                strWhere += " and CarCode='" + txtCarCode.Text + "'";
            }
            DateTime js = txtjs.Value.AddDays(1);
            strWhere += " and Time BETWEEN '" + txtks.Value + "' AND '" + js + "'";
            strSearch = " 1=1 " + strWhere;
            this.pager1.Bind();
        }

        private int pager1_EventPaging(EventPagingArg e)
        {
            return dgvBind();
        }

        private void dgvInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderButtonCell(e))
            {
                ButtonClicked(e);
            }
        }
        private void ButtonClicked(DataGridViewCellEventArgs e)
        {
            String strButtonContent = dgvInfo.Columns[e.ColumnIndex].Name;
            String id = dgvInfo.Rows[e.RowIndex].Cells["编号"].Value.ToString();
            if (strButtonContent.Trim().Equals("修改"))
            {
                AddAccident add = new AddAccident(isDriver, Convert.ToInt32(id),"edit");
                add.ShowDialog();
            }
            else if (strButtonContent.Trim().Equals("删除"))
            {
                //"确定删除？"
                DialogConfirm diaSecc = new DialogConfirm("确定删除此事故信息吗？");
                if (diaSecc.ShowDialog() == DialogResult.OK)
                {
                    Model.Accident model = new Model.Accident();
                    model.Id = Convert.ToInt32(id);
                    //BLL.Accident bll = new BLL.Accident();
                    //bll.Delete(model.Id);
                    //txtCarCode.Text = "";
                    //DialogNotify notify = new DialogNotify("删除事故信息成功！");
                    //notify.ShowDialog();


                    ServiceManipulator.SafeAsyncExecution<AccidentSvc.AccidentServiceSoapClient>(
                       proxy => proxy.Delete(sopaHeader, model.Id),
                       result =>
                       {
                           if (result.Error == null)
                           {
                               PageCommon.Notify("删除事故信息成功！");
                               overlay.FadeOut();Cursor.Current = Cursors.Default;
                           }
                           else
                           {
                               PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                           }
                       },
                       this
                   );
                }
            }
            else if (strButtonContent.Trim().Equals("查看"))
            {
                AddAccident add = new AddAccident(isDriver, Convert.ToInt32(id),"look");
                add.ShowDialog();
            }
            this.pager1.Bind();
        }
        private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
        {
            if (dgvInfo.Columns[cellEvent.ColumnIndex] is
                 DataGridViewButtonColumn &&
                 cellEvent.RowIndex != -1)
            { return true; }
            else { return (false); }
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        #region 车牌号 输入框数据绑定

        string driverName = string.Empty;
        private void txtCarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = this.txtCarCode.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("车牌号不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    DisplayCustomeLIst(strSearch, "车牌号");
                }
            }
        }

        private void DisplayCustomeLIst(string strSearch, string type)
        {
            if (string.IsNullOrEmpty(strSearch))
            {
                return;
            }
            string strSelectWhere = "";
            if (type == "车牌号")
            {
                strSelectWhere = " Code like '%" + strSearch + "%'";
            }
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;
            DataSet ds = null;
            this.dgvDriverInfo.DataSource = null;
            dgvDriverInfo.AutoGenerateColumns = false;
            CarSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CarSvc.SoapHeaderClass();
            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetList(sopaHeader, strSelectWhere),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            dgvDriverInfo.DataSource = ds.Tables[0];
                            this.dgvDriverInfo.Visible = true;
                            this.dgvDriverInfo.Focus();
                        }
                        else
                        {
                            PageCommon.Alert("查找的车辆不存在，请重新查询！");
                            txtCarCode.Text = driverName;
                        }
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

        }

        private void dgvDriveInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvDriverInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvDriverInfo.Rows.Count; )
                {
                    this.dgvDriverInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.txtCarCode.Text = this.dgvDriverInfo.CurrentRow.Cells["车牌号1"].Value.ToString();
                //  driverID = Convert.ToInt32(this.dgvInfo.CurrentRow.Cells["编号"].Value.ToString());
                driverName = this.txtCarCode.Text;
                this.dgvDriverInfo.Visible = false;
            }
        }

        private void dgvInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvDriverInfo.Columns[e.ColumnIndex].Name == "性别")
            {
                if (e.Value != null)
                {
                    if (Convert.ToBoolean(e.Value))
                    {
                        e.Value = "带驾";
                    }
                    else
                    {
                        e.Value = "自驾";
                    }
                }
            }

        }

        #endregion 车牌号 输入框数据绑定

    }
}
